﻿// Copyright (c) ZhangPeihang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Components.Authorization;
using LaserMark.Components.Authentication;

namespace LaserMark.Components.Authentication
{
    public class AppAuthenticationProvider : AuthenticationStateProvider
    {

        private AuthenticationState currentUser;

        public AppAuthenticationProvider(ExternalAuthService service)
        {
            currentUser = new AuthenticationState(service.CurrentUser);

            service.UserChanged += (newUser) =>
            {
                currentUser = new AuthenticationState(newUser);
                NotifyAuthenticationStateChanged(Task.FromResult(currentUser));
            };
        }

        public override Task<AuthenticationState> GetAuthenticationStateAsync() => Task.FromResult(currentUser);
    }
}
